package caiqr.utils

import org.apache.spark.SparkContext
import org.apache.spark.sql.{DataFrame, SQLContext}
import org.apache.spark.rdd.RDD

object AllGoalInputFile {

  // 加载大小球盘口大文件, 返回 DF.
  def load(sc: SparkContext, sqlContext: SQLContext, filename: String): DataFrame ={
    val people = sc.textFile(filename)
    val schemaString = "match_id,company_id,match_time,season_id,match_desc,season_pre,group_pre,host_id,away_id,init_result,curr_result,score,init_big,init_odds,init_small,curr_big,curr_odds,curr_small,init_big_water,init_small_water,curr_big_water,curr_small_water,init_ret,curr_ret,myear,mmonth"
    import org.apache.spark.sql.Row
    import org.apache.spark.sql.types.{StringType, StructField, StructType}
    val schema =
      StructType(
        schemaString.split(",").map(fieldName => StructField(fieldName, StringType, nullable = true)))
    val rowRDD = people.map(_.split("\t")).map(p => Row(p(0), p(1), p(2), p(3), p(4), p(5), p(6), p(7), p(8), p(9), p(10), p(11), p(12), p(13), p(14), p(15), p(16), p(17),p(18),p(19),p(20), p(21), p(22), p(23), p(24), p(25)))
    sqlContext.createDataFrame(rowRDD, schema)
  }


  // 加载需要计算的比赛数据文件
  def load_match_file(sc: SparkContext, sqlContext: SQLContext, filename: String): DataFrame ={
    val people = sc.textFile(filename)
    val schemaString = "match_id,company_id,match_time,season_id,match_desc,season_pre,group_pre,host_id,away_id,init_big,init_odds,init_small,curr_big,curr_odds,curr_small,init_big_water,init_small_water,curr_big_water,curr_small_water,init_ret,curr_ret"
    import org.apache.spark.sql.Row
    import org.apache.spark.sql.types.{StringType, StructField, StructType}
    val schema =
      StructType(
        schemaString.split(",").map(fieldName => StructField(fieldName, StringType, nullable = true)))
    val rowRDD = people.map(_.split("\t")).map(p => Row(p(0), p(1), p(2), p(3), p(4), p(5), p(6), p(7), p(8), p(9),p(10), p(11), p(12), p(13), p(14), p(15), p(16), p(17), p(18), p(19), p(20)))
    sqlContext.createDataFrame(rowRDD, schema)
  }




}
